package org.fhm.zdte.common.service.transmission;

import org.fhm.substrate.annotation.Setup;
import org.fhm.zdte.common.constant.protocol.PacketType;
import org.fhm.zdte.common.pojo.Address;
import org.fhm.zdte.common.protocol.AbstractMessageMonitor;
import org.fhm.zdte.common.protocol.AbstractMessageStandard;
import org.fhm.zdte.common.protocol.AbstractMessageWrappers;
import org.fhm.zdte.common.standard.IChannelDispatcher;
import org.fhm.zdte.common.standard.IThreadManager;
import org.fhm.zdte.common.task.SendDealTask;

import java.util.Map;


/**
 * The abstract layer for cmd.
 *
 * @author 谭波
 * @since 2023/11/6
 */
public abstract class AbstractCmd {

    @Setup
    protected Map<String, AbstractDataDeal> subPacketDataDealMap;

    @Setup
    protected SendDealTask sendDealTask;

    @Setup
    protected IThreadManager iThreadManager;

    @Setup
    protected AbstractMessageMonitor abstractMessageMonitor;

    @Setup("->protocol.name")
    protected AbstractMessageWrappers abstractMessageWrappers;

    @Setup
    protected IChannelDispatcher iChannelDispatcher;

    /**
     * Accept the common processing method of Packet-Message.
     *
     * @param address The address of sender side.
     * @param message Packet-Message.
     */
    public void receiveCmd(Address address, AbstractMessageStandard message) {
        subPacketDataDealMap
                .get(PacketType.getPacketNameBy(message.getPacketType()))
                .consume(address, message);
    }

    /**
     * A common way to handle sending Packet-Message.
     *
     * @param address The address of sender side.
     * @param message Packet-Message.
     */
    public void sendCmd(Address address, AbstractMessageStandard message) {
        iChannelDispatcher.obtainChannelManager(address).sendMessage(message, address);
    }

}
